package com.zenithmind.document.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 文档评分VO
 * 遵循单一职责原则：专注于评分数据展示
 * 遵循开闭原则：支持字段的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档评分VO")
public class DocumentRatingVO {

    @Schema(description = "评分ID", example = "rating123456")
    private String id;

    @Schema(description = "文档ID", example = "doc123456")
    private String documentId;

    @Schema(description = "文档标题", example = "项目需求文档")
    private String documentTitle;

    @Schema(description = "文档类型", example = "PDF")
    private String documentType;

    @Schema(description = "文档分类", example = "技术文档")
    private String documentCategory;

    @Schema(description = "用户ID", example = "user123456")
    private String userId;

    @Schema(description = "用户名", example = "张三")
    private String userName;

    @Schema(description = "用户昵称", example = "小张")
    private String userNickname;

    @Schema(description = "用户头像", example = "https://example.com/avatar.jpg")
    private String userAvatar;

    @Schema(description = "用户等级", example = "高级用户")
    private String userLevel;

    @Schema(description = "评分", example = "4.5")
    private Double rating;

    @Schema(description = "评分描述", example = "很好")
    private String ratingDesc;

    @Schema(description = "评分星级", example = "★★★★☆")
    private String ratingStars;

    @Schema(description = "评论内容", example = "这是一个很好的文档，内容详细且易懂")
    private String comment;

    @Schema(description = "评论摘要", example = "内容详细且易懂...")
    private String commentSummary;

    @Schema(description = "评分维度", example = "[{\"dimension\": \"内容质量\", \"score\": 5}]")
    private String ratingDimensions;

    @Schema(description = "评分维度描述", example = "内容质量: 5分, 易读性: 4分")
    private String ratingDimensionsDesc;

    @Schema(description = "评分标签", example = "有用,详细,清晰")
    private String ratingTags;

    @Schema(description = "评分标签数组", example = "[\"有用\", \"详细\", \"清晰\"]")
    private String[] ratingTagArray;

    @Schema(description = "评分类型", example = "GENERAL")
    private String ratingType;

    @Schema(description = "评分类型描述", example = "普通评分")
    private String ratingTypeDesc;

    @Schema(description = "评分来源", example = "WEB")
    private String ratingSource;

    @Schema(description = "评分来源描述", example = "网页端")
    private String ratingSourceDesc;

    @Schema(description = "是否匿名", example = "false")
    private Boolean isAnonymous;

    @Schema(description = "推荐给他人", example = "true")
    private Boolean recommendToOthers;

    @Schema(description = "推荐理由", example = "内容全面，适合新手学习")
    private String recommendReason;

    @Schema(description = "使用场景", example = "学习参考")
    private String usageScenario;

    @Schema(description = "阅读时长（分钟）", example = "30")
    private Integer readingDuration;

    @Schema(description = "阅读时长描述", example = "30分钟")
    private String readingDurationDesc;

    @Schema(description = "文档版本", example = "v1.2")
    private String documentVersion;

    @Schema(description = "评分权重", example = "1.0")
    private Double ratingWeight;

    @Schema(description = "评分置信度", example = "0.95")
    private Double confidence;

    @Schema(description = "置信度描述", example = "高置信度")
    private String confidenceDesc;

    @Schema(description = "情感倾向", example = "POSITIVE")
    private String sentiment;

    @Schema(description = "情感倾向描述", example = "正面")
    private String sentimentDesc;

    @Schema(description = "情感分数", example = "0.8")
    private Double sentimentScore;

    @Schema(description = "关键词", example = "质量,内容,实用")
    private String keywords;

    @Schema(description = "关键词数组", example = "[\"质量\", \"内容\", \"实用\"]")
    private String[] keywordArray;

    @Schema(description = "改进建议", example = "可以增加更多实例")
    private String improvementSuggestions;

    @Schema(description = "相关文档推荐", example = "doc789,doc101")
    private String relatedDocuments;

    @Schema(description = "相关文档数组", example = "[\"doc789\", \"doc101\"]")
    private String[] relatedDocumentArray;

    @Schema(description = "评分上下文", example = "{\"project\": \"ProjectA\"}")
    private String ratingContext;

    @Schema(description = "设备信息", example = "PC")
    private String deviceInfo;

    @Schema(description = "IP地址", example = "192.168.1.100")
    private String ipAddress;

    @Schema(description = "地理位置", example = "北京市")
    private String location;

    @Schema(description = "用户代理", example = "Mozilla/5.0...")
    private String userAgent;

    @Schema(description = "浏览器信息", example = "Chrome 120.0")
    private String browserInfo;

    @Schema(description = "扩展属性", example = "{\"category\": \"技术文档\"}")
    private String extendedProperties;

    @Schema(description = "备注", example = "通过搜索找到的文档")
    private String remarks;

    @Schema(description = "更新原因", example = "重新阅读后调整评分")
    private String updateReason;

    @Schema(description = "审核状态", example = "APPROVED")
    private String reviewStatus;

    @Schema(description = "审核状态描述", example = "已审核")
    private String reviewStatusDesc;

    @Schema(description = "审核意见", example = "评分合理")
    private String reviewComment;

    @Schema(description = "审核人", example = "审核员")
    private String reviewerName;

    @Schema(description = "审核时间", example = "2025-01-09T11:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime reviewTime;

    @Schema(description = "是否置顶", example = "false")
    private Boolean isPinned;

    @Schema(description = "是否公开", example = "true")
    private Boolean isPublic;

    @Schema(description = "状态", example = "ACTIVE")
    private String status;

    @Schema(description = "状态描述", example = "活跃")
    private String statusDesc;

    @Schema(description = "点赞数", example = "15")
    private Integer likeCount;

    @Schema(description = "举报数", example = "0")
    private Integer reportCount;

    @Schema(description = "回复数", example = "3")
    private Integer replyCount;

    @Schema(description = "是否已点赞", example = "false")
    private Boolean isLiked;

    @Schema(description = "是否已举报", example = "false")
    private Boolean isReported;

    @Schema(description = "质量分数", example = "85")
    private Integer qualityScore;

    @Schema(description = "质量等级", example = "HIGH", allowableValues = {"LOW", "MEDIUM", "HIGH", "EXCELLENT"})
    private String qualityLevel;

    @Schema(description = "影响力分数", example = "120")
    private Integer influenceScore;

    @Schema(description = "可信度分数", example = "90")
    private Integer credibilityScore;

    @Schema(description = "创建时间", example = "2025-01-09T10:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "创建时间描述", example = "2小时前")
    private String createTimeDesc;

    @Schema(description = "更新时间", example = "2025-01-09T10:35:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "更新时间描述", example = "1小时前")
    private String updateTimeDesc;

    /**
     * 获取评分描述
     */
    public String getRatingDescription() {
        if (rating == null) return "未评分";
        
        if (rating >= 4.5) return "非常好";
        else if (rating >= 4.0) return "很好";
        else if (rating >= 3.5) return "好";
        else if (rating >= 3.0) return "一般";
        else if (rating >= 2.0) return "较差";
        else return "很差";
    }

    /**
     * 获取评分星级
     */
    public String getRatingStars() {
        if (rating == null) return "";
        
        int fullStars = rating.intValue();
        boolean hasHalfStar = (rating - fullStars) >= 0.5;
        
        StringBuilder stars = new StringBuilder();
        for (int i = 0; i < fullStars; i++) {
            stars.append("★");
        }
        if (hasHalfStar) {
            stars.append("☆");
        }
        for (int i = fullStars + (hasHalfStar ? 1 : 0); i < 5; i++) {
            stars.append("☆");
        }
        
        return stars.toString();
    }

    /**
     * 获取评分类型描述
     */
    public String getRatingTypeDescription() {
        if (ratingType == null) return "";
        
        switch (ratingType.toUpperCase()) {
            case "GENERAL": return "普通评分";
            case "EXPERT": return "专家评分";
            case "PEER_REVIEW": return "同行评议";
            case "AUTOMATED": return "自动评分";
            default: return ratingType;
        }
    }

    /**
     * 获取评分来源描述
     */
    public String getRatingSourceDescription() {
        if (ratingSource == null) return "";
        
        switch (ratingSource.toUpperCase()) {
            case "WEB": return "网页端";
            case "MOBILE": return "移动端";
            case "API": return "API接口";
            case "SYSTEM": return "系统自动";
            default: return ratingSource;
        }
    }

    /**
     * 获取情感倾向描述
     */
    public String getSentimentDescription() {
        if (sentiment == null) return "";
        
        switch (sentiment.toUpperCase()) {
            case "POSITIVE": return "正面";
            case "NEUTRAL": return "中性";
            case "NEGATIVE": return "负面";
            default: return sentiment;
        }
    }

    /**
     * 获取置信度描述
     */
    public String getConfidenceDescription() {
        if (confidence == null) return "";
        
        if (confidence >= 0.9) return "高置信度";
        else if (confidence >= 0.7) return "中等置信度";
        else if (confidence >= 0.5) return "低置信度";
        else return "极低置信度";
    }

    /**
     * 获取审核状态描述
     */
    public String getReviewStatusDescription() {
        if (reviewStatus == null) return "";
        
        switch (reviewStatus.toUpperCase()) {
            case "PENDING": return "待审核";
            case "APPROVED": return "已审核";
            case "REJECTED": return "已拒绝";
            default: return reviewStatus;
        }
    }

    /**
     * 获取状态描述
     */
    public String getStatusDescription() {
        if (status == null) return "";
        
        switch (status.toUpperCase()) {
            case "ACTIVE": return "活跃";
            case "INACTIVE": return "非活跃";
            case "HIDDEN": return "已隐藏";
            case "DELETED": return "已删除";
            default: return status;
        }
    }

    /**
     * 获取阅读时长描述
     */
    public String getReadingDurationDescription() {
        if (readingDuration == null || readingDuration <= 0) return "未知";
        
        if (readingDuration < 60) {
            return readingDuration + "分钟";
        } else {
            int hours = readingDuration / 60;
            int minutes = readingDuration % 60;
            return hours + "小时" + (minutes > 0 ? minutes + "分钟" : "");
        }
    }

    /**
     * 获取评论摘要
     */
    public String getCommentSummary() {
        if (comment == null || comment.trim().isEmpty()) return "";
        
        if (comment.length() <= 50) {
            return comment;
        } else {
            return comment.substring(0, 47) + "...";
        }
    }

    /**
     * 获取时间描述
     */
    public String getTimeDescription(LocalDateTime time) {
        if (time == null) return "";
        
        LocalDateTime now = LocalDateTime.now();
        long minutes = ChronoUnit.MINUTES.between(time, now);
        
        if (minutes < 1) return "刚刚";
        else if (minutes < 60) return minutes + "分钟前";
        else if (minutes < 1440) return (minutes / 60) + "小时前";
        else if (minutes < 43200) return (minutes / 1440) + "天前";
        else return time.toLocalDate().toString();
    }

    /**
     * 获取评分标签数组
     */
    public String[] getRatingTagArray() {
        if (ratingTags == null || ratingTags.trim().isEmpty()) {
            return new String[0];
        }
        return ratingTags.split(",");
    }

    /**
     * 获取关键词数组
     */
    public String[] getKeywordArray() {
        if (keywords == null || keywords.trim().isEmpty()) {
            return new String[0];
        }
        return keywords.split(",");
    }

    /**
     * 获取相关文档数组
     */
    public String[] getRelatedDocumentArray() {
        if (relatedDocuments == null || relatedDocuments.trim().isEmpty()) {
            return new String[0];
        }
        return relatedDocuments.split(",");
    }

    /**
     * 是否为高分评价
     */
    public boolean isHighRating() {
        return rating != null && rating >= 4.0;
    }

    /**
     * 是否为低分评价
     */
    public boolean isLowRating() {
        return rating != null && rating <= 2.0;
    }

    /**
     * 是否有评论
     */
    public boolean hasComment() {
        return comment != null && !comment.trim().isEmpty();
    }

    /**
     * 是否有标签
     */
    public boolean hasTags() {
        return ratingTags != null && !ratingTags.trim().isEmpty();
    }

    /**
     * 是否有维度评分
     */
    public boolean hasDimensions() {
        return ratingDimensions != null && !ratingDimensions.trim().isEmpty();
    }

    /**
     * 是否推荐给他人
     */
    public boolean isRecommended() {
        return recommendToOthers != null && recommendToOthers;
    }

    /**
     * 是否为专家评分
     */
    public boolean isExpertRating() {
        return "EXPERT".equalsIgnoreCase(ratingType);
    }

    /**
     * 是否为匿名评分
     */
    public boolean isAnonymousRating() {
        return isAnonymous != null && isAnonymous;
    }

    /**
     * 是否为正面评价
     */
    public boolean isPositiveRating() {
        return "POSITIVE".equalsIgnoreCase(sentiment) || (rating != null && rating >= 3.5);
    }

    /**
     * 是否为负面评价
     */
    public boolean isNegativeRating() {
        return "NEGATIVE".equalsIgnoreCase(sentiment) || (rating != null && rating <= 2.5);
    }

    /**
     * 是否需要审核
     */
    public boolean needsReview() {
        return "PENDING".equalsIgnoreCase(reviewStatus);
    }

    /**
     * 是否已审核通过
     */
    public boolean isApproved() {
        return "APPROVED".equalsIgnoreCase(reviewStatus);
    }

    /**
     * 是否为高质量评分
     */
    public boolean isHighQuality() {
        return qualityScore != null && qualityScore >= 80;
    }

    /**
     * 是否有影响力
     */
    public boolean isInfluential() {
        return influenceScore != null && influenceScore >= 100;
    }

    /**
     * 是否可信
     */
    public boolean isCredible() {
        return credibilityScore != null && credibilityScore >= 70;
    }

    /**
     * 计算综合分数
     */
    public double calculateOverallScore() {
        double score = 0.0;
        
        // 评分权重 40%
        if (rating != null) {
            score += (rating / 5.0) * 0.4;
        }
        
        // 质量分数权重 30%
        if (qualityScore != null) {
            score += (qualityScore / 100.0) * 0.3;
        }
        
        // 可信度权重 20%
        if (credibilityScore != null) {
            score += (credibilityScore / 100.0) * 0.2;
        }
        
        // 影响力权重 10%
        if (influenceScore != null) {
            score += Math.min(influenceScore / 200.0, 1.0) * 0.1;
        }
        
        return Math.min(score, 1.0);
    }
}
